package com.cos.shop.web.servlet;

import cn.dsna.util.images.ValidateCode;
import com.cos.shop.common.Const;
import com.cos.shop.util.StrUtils;
import com.cos.shop.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: SystemController
 * Author: saber
 * Date:2022/9/16 22:31
 */
@WebServlet("/sys")
public class SystemController extends BaseServlet {

    public void code(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ValidateCode vs = new ValidateCode(120, 30, 4, 30);

        String code = vs.getCode();

        request.getSession().setAttribute(Const.SystemConst.VALI_CODE, code);

        vs.write(response.getOutputStream());

    }

    public String checkCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code =request.getParameter("code");

        String attribute = (String) request.getSession().getAttribute(Const.SystemConst.VALI_CODE);

        if (StringUtils.isNotEmpty(code)&&code.equalsIgnoreCase(attribute)){
            return Const.SUCCESS;
        }else {
            return Const.FAIL;
        }

    }
}